package cn.ekuma.pos.pda.inventory;

import java.util.HashMap;
import java.util.List;

import android.app.ProgressDialog;
import android.view.View;
import android.view.ViewGroup;
import cn.ekuma.data.dao.I_DataLogic;
import cn.ekuma.data.dao.bean.I_ViewBean;
import cn.ekuma.data.ui.android.AbstractViewBeanDetailActivate;
import cn.ekuma.epos.bean.util.CustomerHelper;
import cn.ekuma.epos.datalogic.DataLogicPDA;

import com.openbravo.bean.Customer;
import com.openbravo.bean.crm.CustomerAddress;
import com.openbravo.bean.crm.CustomerContact;
import com.openbravo.bean.crm.CustomerPeople;
import com.openbravo.bean.crm.CustomerStoreValueCard;
import com.openbravo.bean.crm.service.Telephone;
import com.openbravo.data.basic.BasicException;

public class CustomerDetailViewActivate extends AbstractViewBeanDetailActivate<Customer>{

	
	public static final String[] GROUPNAMES={
		"联系人",
		"地址",
		"联系记录",
		"储值卡",
		"电话营销"
	};
	
	protected void initData() {
		final ProgressDialog dialog = ProgressDialog.show(this, "", 
		        "查旬中，请稍等 …", true, true);
		new Thread(){
			@SuppressWarnings({ "unchecked"})
			
			public void run(){
				I_DataLogic dlERP=DataLogicPDA.getDataLogic();
				HashMap<String,List<I_ViewBean>> groupLinkData=new HashMap<String,List<I_ViewBean>>();
				String groupName1;
				try {
					List<I_ViewBean> lists=dlERP.listWithParentClassAndKey(CustomerPeople.class.getName(), Customer.class.getName(),obj.getId());
					groupName1=GROUPNAMES[0];
					if(lists!=null&&!lists.isEmpty()){
						groupLinkData.put(groupName1, lists);
					}

					lists=dlERP.listWithParentClassAndKey(CustomerAddress.class.getName(), Customer.class.getName(),obj.getId());
					groupName1=GROUPNAMES[1];
					if(lists!=null&&!lists.isEmpty()){
						groupLinkData.put(groupName1, lists);
					}
					
					lists=dlERP.listWithParentClassAndKey(CustomerContact.class.getName(), Customer.class.getName(),obj.getId());
					groupName1=GROUPNAMES[2];
					if(lists!=null&&!lists.isEmpty()){
						groupLinkData.put(groupName1, lists);
					}

					lists=dlERP.listWithParentClassAndKey(CustomerStoreValueCard.class.getName(), Customer.class.getName(),obj.getId());
					groupName1=GROUPNAMES[3];
					if(lists!=null&&!lists.isEmpty()){
						groupLinkData.put(groupName1, lists);
					}
					
					lists=dlERP.listWithParentClassAndKey(Telephone.class.getName(), Customer.class.getName(),obj.getId());
					groupName1=GROUPNAMES[4];
					if(lists!=null&&!lists.isEmpty()){
						groupLinkData.put(groupName1, lists);
					}
					mHandler.obtainMessage(MSG_SUCCESS,groupLinkData).sendToTarget();
				} catch (BasicException e) {
					mHandler.obtainMessage(MSG_FAILURE).sendToTarget();
				}
				dialog.dismiss(); 
			}
			}.start();		
	}
	
	
	public View getVeiwBeanDetailView(Customer obj) {
		return createTextView(CustomerHelper.printName(obj));
	}

	
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		String groupName1=groupName.get(groupPosition);
		int realPosition=getRealPosition(GROUPNAMES,groupName1);
		I_ViewBean obj=groupLinkData.get(groupName1).get(childPosition);
		View retView=null;
		switch(realPosition){
		 case 0:
			 CustomerPeople productBom=(CustomerPeople)obj;
			retView=createTextView(productBom.getName());
			break;
		case 1:
			CustomerAddress temp=(CustomerAddress)obj;
			retView=createTextView(temp.getAddress());
			break;
		case 2:
			CustomerContact productCode=(CustomerContact)obj;
			retView=createTextView(productCode.getMemo());
			break;
		case 3:
			CustomerStoreValueCard productAuxiliar=(CustomerStoreValueCard)obj;
			retView=createTextView(productAuxiliar.getCardNum());
		  break;
		case 4:
			Telephone productStockLevel=(Telephone)obj;
			retView=createTextView(productStockLevel.getState());
			break;
		}
		return retView;
	}
	
}

